// pages/class-look/index.js
const handler = require("../../utils/function.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    current: '0',
    current_scroll: '0',
    showTimeContainer: false,
    star: [5, 4, 3 , 2 , 1 ],//时间选择器
    express: [{name:"1-10",val:0},{name:"10-20",val:1},{name:"20-30",val:2},{name:"30-40",val:3}],
    evaluate: [{name:"好评",val:2},{name:"中评",val:1},{name:"差评",val:0}],
    homeHandlerShow: true,
    spinShow: true,
    visible1: false,
    actions1: [
      {
        name: '预约课程',
      },
      {
        name: '去分享',
        icon: 'share',
        openType: 'share'
      }
    ],
    scrollTop: false,
    
    categoryList:[],
    tearchCategoryData:[],
    current_page:1,
    oldData:[],
    searchContet:'',
    starValue: -1,
    expressValue: -1
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // this.get_data();
    this.initCategoryData();
    this.initData();
  },
  //初始化类别
  initCategoryData: function () {
    var para = {
    };
    var that = this;
    handler.requestData("categoryList", para, function (res) {
      if (res.success == true) {
        that.setData({
          spinShow: false,
          categoryList: res.data
        
        })
        wx.hideNavigationBarLoading(); //完成停止加载
        wx.stopPullDownRefresh(); //停止下拉刷新
      } else {
        wx.showToast({
          title: res.message,
          icon: 'loading'
        })
      }
     
    });
  },
  searchContet:function(e){
   var content = e.detail.value;
    this.setData({
      searchContet: content,
      starValue: -1,
      expressValue: -1
    })
    this.initData();
  },
  
  initData:function(){
    var that = this;
    var para = {
      current_page: that.data.current_page,
      category_id:that.data.current_scroll,
      searchContet: that.data.searchContet,
      starValue: that.data.starValue,
      expressValue: that.data.expressValue
    };

    handler.requestData("tearchCategory", para, function (res) {
      if (res.success == true) {
        if (that.data.current_scroll == 0) {
          that.setData({
            tearchCategoryData: res.data
          })
        } else if (that.data.current_scroll > 0) {
          that.setData({
            tearchCategoryData: { data: res.data.filterData }
          })
        }
        // that.setData({
        //   tearchCategoryData: res.data,
        
        // })
        wx.hideNavigationBarLoading(); //完成停止加载
        wx.stopPullDownRefresh(); //停止下拉刷新
      } else {
        wx.showToast({
          title: res.message,
          icon: 'loading'
        })
      }
      // console.log(that.data.tearchCategoryData);
    });
  },
  // get_data() {
  //   var that = this;
  //   var i = 0;
  //   var time = setInterval(() => {
  //     if (i < 5) { i++; console.log() }
  //     else { clearInterval(time); that.setData({ spinShow: false }) }
  //   }, 1000)
  // },
  // 监听事件
  handleChangeScroll({ detail }) {
    var that = this;
    that.setData({
      current_scroll: detail.key,
      starValue: -1,
      expressValue: -1
    });
    var para = {
      current_page: that.data.current_page,
      category_id: that.data.current_scroll
    };
    that.initData();
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },
  //时间样式开始
  selectTimeShowTimeHandler: function () {
    this.setData({ showTimeContainer: true, homeHandlerShow: false });
    
  },
  // getTimeHandlerCancle 预约时间取消样式
  getTimeHandlerCancle: function () {
    this.setData({ showTimeContainer: false, homeHandlerShow: true });
  },
  // getTimeHandlerOk 预约时间确定样式
  getTimeHandlerOk: function () {
    this.setData({ showTimeContainer: false, homeHandlerShow: true });
    this.initData();
  },
  // 绑定时间定时器
  bindTimeChange: function (e) {
    var piker = e.detail.value;
    var starIndex = piker[0];
    var experenceIndex = piker[1];
    var commentIndex = piker[2];

    var starValue = this.data.star[starIndex];
    var expressValue = this.data.express[experenceIndex]['val'];
    this.setData({
      starValue: starValue,
      expressValue: expressValue
    })
    // var evaluateValue = this.data.evaluate[commentIndex]['val'];
   
  },
  // 页面跳转
  handler_next_page(e) {
    handler.intoPageHandler(e.currentTarget.dataset.url);
  },
  //页面滚动执行方式
  onPageScroll(event) {
    // console.log(event);
    event.scrollTop >= 83 && this.setData({ scrollTop: true })
    event.scrollTop < 83 && this.setData({ scrollTop: false })
  },
  handleOpen1(e) {
    var user_id = e.currentTarget.dataset.userid;
    this.setData({
      visible1: true,
      tearchid: user_id
    });
  },
  handleCancel1() {
    this.setData({
      visible1: false
    });
  },
  onShareAppMessage() {
    return {
      title: 'iView Weapp',
      imageUrl: 'https://file.iviewui.com/iview-weapp-logo.png'
    };
  },
  handleClickItem1({ detail }) {
    handler.intoPageHandler("../teacher-detail/index?tearchid="+this.data.tearchid);
  },
  onChange(event) {
    console.log(event.detail, 'click right menu callback data')
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    wx.showNavigationBarLoading() //在标题栏中显示加载
    this.initData();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    //上拉加载更多
    var temppage = this.data.current_page;
    var total_page = this.data.tearchCategoryData.total;
    if (total_page < this.data.current_page * 10) {
        console.log("已经是全部数据");
    } else {
        this.setData({
          current_page: temppage + 1
        })
        wx.showNavigationBarLoading(); //在标题栏中显示加载
        this.initData();
    } 
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },
  formSubmit: function (e) {
    var user_id = wx.getStorageSync('user_info').id;
    var formid = e.detail.formId;
    console.log(formid);
    if (user_id && formid) {
      var para = {
        form_id: formid,
        user_id: user_id
      };
      var that = this;
      handler.requestData("storageFormid", para, function (res) { console.log(res) });
    }
  }
})